<template>
  <div class="container" ref="container">
    <el-card shadow="always" :body-style="{ padding: '20px' }" class="card-container" ref="qwe">
      <v-md-editor v-model="text" ref="editor" :height="editorHeight" @save="Save"></v-md-editor>
    </el-card>
    <el-dialog v-model="dialogVisible" title="Blog初始化" width="50%">
      <template #default>
        <div class="dialog-body">
          <div class="form-item">
            <TitleBar title="Blog标题" class="titleBar" icon="bi bi-card-heading" />
            <el-input v-model="InitBlog.title" placeholder="文章标题" />
          </div>
          <div class="form-item">
            <TitleBar title="Blog简介" class="titleBar" icon="bi bi-blockquote-right" />
            <el-input
              v-model="InitBlog.introduce"
              :autosize="{ minRows: 2, maxRows: 4 }"
              type="textarea"
              placeholder="Please input"
            >
            </el-input>
          </div>

          <div class="form-item">
            <TitleBar title="Blog分类" class="titleBar" icon="bi bi-microsoft" />
            <ChipList :tagArray="TypeArray" @allSelectChips="SelectBlogType" chipType="radio">
              <template #chip="{ model }">
                {{ model.type }}
              </template>
            </ChipList>
          </div>

          <div class="form-item">
            <TitleBar title="Blog标签" class="titleBar" icon="bi bi-bookmark-star-fill" />
            <ChipList :tagArray="TagArray" @allSelectChips="allSelectChips">
              <template #chip="{ model }">
                {{ model.tag }}
              </template>
            </ChipList>
          </div>

          <div class="form-item">
            <TitleBar title="Blog封面" class="titleBar" icon="bi bi-card-image" />
            <InputFile @CoverFile="getCoverFile"></InputFile>
          </div>
        </div>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="commitBlog">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import TitleBar from "@/components/common/TitleBar.vue";
import ChipList from "@/components/manager/ChipList.vue";
import InputFile from "@/components/common/InputFile.vue";
export default {
  data() {
    return {
      text: "",
      editorHeight: "0px",
      normalHeight: 0,
      dialogVisible: false,
      InitBlog: {
        title: "",
        introduce: "",
      },
      imageUrl: false,
      TagArray: [],
      TypeArray: [],
      BlogMessage: {
        cover: {},
        type: "",
        tagStr: "",
        file: {},
      },
    };
  },
  created() {
    window.onresize = () => {
      const height = this.$refs.container.offsetHeight - 40;
      if (height < this.normalHeight) {
        this.editorHeight = height + "px";
      } else {
        this.editorHeight = this.normalHeight + "px";
      }
    };
  },
  mounted() {
    const height = this.$refs.container.offsetHeight - 40;
    this.normalHeight = height;
    this.editorHeight = height + "px";
  },
  methods: {
    Save(text, html) {
      const file = new File([text], new Date().getTime() + ".md", {
        type: "text/plain",
      });
      this.BlogMessage.file = file;
      this.dialogVisible = true;
    },
    allSelectChips(ChipArray) {
      // 获取Blog Tag

      this.BlogMessage.tagStr = "";
      ChipArray.forEach((item) => {
        this.BlogMessage.tagStr += item.value.id + ",";
      });
      this.BlogMessage.tagStr = this.BlogMessage.tagStr.substring(
        0,
        this.BlogMessage.tagStr.length - 1
      );
    },
    SelectBlogType(value) {
      // 获取BlogType
      if (value.length > 0) {
        this.BlogMessage.type = value[0].value.id;
      } else {
        this.BlogMessage.type = undefined;
      }
    },
    getCoverFile(cover) {
      // 获取Blog封面
      this.BlogMessage.cover = cover;
    },
    commitBlog() {
      this.dialogVisible = false;
      var data = new FormData();
      data.append("title", this.InitBlog.title);
      data.append("introduction", this.InitBlog.introduce);
      data.append("cover", this.BlogMessage.cover);
      data.append("typeId", this.BlogMessage.type);
      data.append("tagStrId", this.BlogMessage.tagStr);
      data.append("userId", 1);
      data.append("file", this.BlogMessage.file);
      const response = this.$http.post("/api/manager/blog/WriteBlog", data).catch(function (error) {
        console.log(error);
      });
      console.log(response);
    },
  },
  components: {
    TitleBar,
    ChipList,
    InputFile,
  },
  async created() {
    const response = await this.$http.get("/api/manager/article/tag/AllTags");
    this.TagArray = response.data.data;
    const responseTwo = await this.$http.get("/api/manager/article/type/AllType");
    this.TypeArray = responseTwo.data.data;
  },
};
</script>

<style scoped>
.container,
.card-container {
  height: 100%;
}

.form-item {
  margin-bottom: 1rem;
}
.titleBar {
  justify-content: left;
  margin-bottom: 0.5rem;
  color: #34495e;
  font-family: "Monaco", sans-serif !important;
  font-weight: 600;
}
:deep() .el-dialog {
  border-radius: 1rem;
}
:deep() .el-dialog__title {
  font-size: 1.5rem;
  font-weight: bold;
}
</style>
